home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / ecb_enc.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  6KB  |  204 lines

  1. /* lib/des/ecb_enc.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include "des_locl.h"
  49. #include "spr.h"
  50.  
  51. const char *DES_version="libdes v 3.21 - 95/11/21 - eay";
  52.  
  53. void des_ecb_encrypt(input, output, ks, encrypt)
  54. des_cblock (*input);
  55. des_cblock (*output);
  56. des_key_schedule ks;
  57. int encrypt;
  58.     {
  59.     register unsigned long l0,l1;
  60.     register unsigned char *in,*out;
  61.     unsigned long ll[2];
  62.  
  63.     in=(unsigned char *)input;
  64.     out=(unsigned char *)output;
  65.     c2l(in,l0); ll[0]=l0;
  66.     c2l(in,l1); ll[1]=l1;
  67.     des_encrypt(ll,ks,encrypt);
  68.     l0=ll[0]; l2c(l0,out);
  69.     l1=ll[1]; l2c(l1,out);
  70.     l0=l1=ll[0]=ll[1]=0;
  71.     }
  72.  
  73. void des_encrypt(data, ks, encrypt)
  74. unsigned long *data;
  75. des_key_schedule ks;
  76. int encrypt;
  77.     {
  78.     register unsigned long l,r,t,u;
  79. #ifdef DES_USE_PTR
  80.     register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  81. #endif
  82. #ifdef MSDOS
  83.     union fudge {
  84.         unsigned long  l;
  85.         unsigned short s[2];
  86.         unsigned char  c[4];
  87.         } U,T;
  88. #endif
  89.     register int i;
  90.     register unsigned long *s;
  91.  
  92.     u=data[0];
  93.     r=data[1];
  94.  
  95.     IP(u,r);
  96.     /* Things have been modified so that the initial rotate is
  97.      * done outside the loop.  This required the
  98.      * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  99.      * One perl script later and things have a 5% speed up on a sparc2.
  100.      * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  101.      * for pointing this out. */
  102.     l=(r<<1)|(r>>31);
  103.     r=(u<<1)|(u>>31);
  104.  
  105.     /* clear the top bits on machines with 8byte longs */
  106.     l&=0xffffffffL;
  107.     r&=0xffffffffL;
  108.  
  109.     s=(unsigned long *)ks;
  110.     /* I don't know if it is worth the effort of loop unrolling the
  111.      * inner loop */
  112.     if (encrypt)
  113.         {
  114.         for (i=0; i<32; i+=4)
  115.             {
  116.             D_ENCRYPT(l,r,i+0); /*  1 */
  117.             D_ENCRYPT(r,l,i+2); /*  2 */
  118.             }
  119.         }
  120.     else
  121.         {
  122.         for (i=30; i>0; i-=4)
  123.             {
  124.             D_ENCRYPT(l,r,i-0); /* 16 */
  125.             D_ENCRYPT(r,l,i-2); /* 15 */
  126.             }
  127.         }
  128.     l=(l>>1)|(l<<31);
  129.     r=(r>>1)|(r<<31);
  130.     /* clear the top bits on machines with 8byte longs */
  131.     l&=0xffffffffL;
  132.     r&=0xffffffffL;
  133.  
  134.     FP(r,l);
  135.     data[0]=l;
  136.     data[1]=r;
  137.     l=r=t=u=0;
  138.     }
  139.  
  140. void des_encrypt2(data, ks, encrypt)
  141. unsigned long *data;
  142. des_key_schedule ks;
  143. int encrypt;
  144.     {
  145.     register unsigned long l,r,t,u;
  146. #ifdef DES_USE_PTR
  147.     register unsigned char *des_SP=(unsigned char *)des_SPtrans;
  148. #endif
  149. #ifdef MSDOS
  150.     union fudge {
  151.         unsigned long  l;
  152.         unsigned short s[2];
  153.         unsigned char  c[4];
  154.         } U,T;
  155. #endif
  156.     register int i;
  157.     register unsigned long *s;
  158.  
  159.     u=data[0];
  160.     r=data[1];
  161.  
  162.     /* Things have been modified so that the initial rotate is
  163.      * done outside the loop.  This required the
  164.      * des_SPtrans values in sp.h to be rotated 1 bit to the right.
  165.      * One perl script later and things have a 5% speed up on a sparc2.
  166.      * Thanks to Richard Outerbridge <71755.204@CompuServe.COM>
  167.      * for pointing this out. */
  168.     l=(r<<1)|(r>>31);
  169.     r=(u<<1)|(u>>31);
  170.  
  171.     /* clear the top bits on machines with 8byte longs */
  172.     l&=0xffffffffL;
  173.     r&=0xffffffffL;
  174.  
  175.     s=(unsigned long *)ks;
  176.     /* I don't know if it is worth the effort of loop unrolling the
  177.      * inner loop */
  178.     if (encrypt)
  179.         {
  180.         for (i=0; i<32; i+=4)
  181.             {
  182.             D_ENCRYPT(l,r,i+0); /*  1 */
  183.             D_ENCRYPT(r,l,i+2); /*  2 */
  184.             }
  185.         }
  186.     else
  187.         {
  188.         for (i=30; i>0; i-=4)
  189.             {
  190.             D_ENCRYPT(l,r,i-0); /* 16 */
  191.             D_ENCRYPT(r,l,i-2); /* 15 */
  192.             }
  193.         }
  194.     l=(l>>1)|(l<<31);
  195.     r=(r>>1)|(r<<31);
  196.     /* clear the top bits on machines with 8byte longs */
  197.     l&=0xffffffffL;
  198.     r&=0xffffffffL;
  199.  
  200.     data[0]=l;
  201.     data[1]=r;
  202.     l=r=t=u=0;
  203.     }
  204.